﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml.Linq;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL.Forms;
using UCL.ADF;
using UCL;

namespace SoIM.Client
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>
    ///     2013-7-4 sutroon
    ///     tvList: Size(149, 399), Loc(0, 59)
    /// </remarks>
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            this.MoveByControl_(this);
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.DesktopLocation = ScreenHelper.GetScreenPoint(this.Width, this.Height);
            // 按钮事件配置
            btnEventConfig();

            lblTitle.Text = "Hi, " + AppDataHelper.User.UserName;

            this.LoadServerUsers();

            // 异步下载用户数据
            // new Common.frmAsyncTask() { CompletedHandler = () => { ucLst.LoadData(); } }.Show();

            // 本机当成服务器
            new frmServicesControl() { MessageReceivedHandler = MessageReceivedEvent };

        }
        private void MessageReceivedEvent(string msg, string ip, int port)
        {
            ucUserNode u = ucLst.FindUserNodeByIP(ip);
            if (u == null)
            {
                MessageBox.Show("IP地址有误,用户不存在");
                return;
            }
            new frmMessageLAN() { TargetPort = ip, TargetIP = ip, UserName = u.NodeText, UserFace = Image.FromFile(u.ImagePath) }.Show();
        }

        private void LoadLANUsers()
        {

        }

        ucUserTree ucLst;
        private void LoadServerUsers()
        {
            ucLst = new ucUserTree() { Dock = DockStyle.Fill };
            ucLst.uc_Init();
            panelMain.Controls.Clear();
            panelMain.Controls.Add(ucLst);
        }

        // 按钮事件配置
        private void btnEventConfig()
        {
            btnClose.Click += new EventHandler(btn_Click);
        }
        private void btn_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btnClose":
                    notifyIcon1.Dispose();
                    Application.Exit();
                    break;

            }
        }
    }
}
